package com.dfk.bs.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dfk.bs.common.constants.ResultCodeEnum;
import com.dfk.bs.common.exception.BsException;
import com.dfk.bs.common.utils.FirstChar;
import com.dfk.bs.common.vo.R;
import com.dfk.bs.entity.Eleapp;
import com.dfk.bs.entity.Housetype;
import com.dfk.bs.entity.User;
import com.dfk.bs.service.HousetypeService;
import com.dfk.bs.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author DDK
 * @since 2019-12-12
 */

@Api("业主信息管理")
@CrossOrigin
@RestController
@RequestMapping("/ddkbishe/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("所有业主信息列表")
    @GetMapping("/list")
    public R list(){

        List<User> list = userService.list(null);

        return R.ok()
                .message("查询业主信息列表成功")
                .data("items",list);
    }

    @ApiOperation("新增业主")
    @PostMapping("/save")
    public R save(
            @ApiParam(name="user",value = "业主基本信息",required = true)
            @RequestBody User user){
        String password= FirstChar.ToFirstChar(user.getName());
        String username= FirstChar.toHanyuPinyin(user.getName());
        user.setUsername(username);
        user.setPassword(password+user.getHousecode());
        userService.save(user);
        return R.ok()
                .message("新增业主成功");
    }

    @ApiOperation("根据业主id删除业主")
    @DeleteMapping("/remove/{userid}")
    public R removeById(
            @ApiParam(name = "userid",value = "业主id", required = true)
            @PathVariable String userid){
        userService.removeById(userid);
        return R.ok()
                .message("删除业主成功");
    }


    @ApiOperation("分页条件查询业主列表")
    @PostMapping("/pageQuery")
    public R pageQuery(@RequestBody Map<String, Object> params){
        Integer page = (Integer) params.get("page");
        Integer limit = (Integer) params.get("limit");
        String selectname = (String) params.get("selectname");
        //业务逻辑错误，抛出自定义异常
        if(page <= 0 || limit <= 0){
            throw new BsException(ResultCodeEnum.PARAM_ERROR);
        }
        Page<User> pageParam=new Page<>(page,limit);
        /*已经把查询的每一页的信息放到 pageParam中*/
        userService.pageQuery(pageParam,selectname);
        List<User> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok()
                .message("分页条件查询业主列表成功")
                .data("total",total)
                .data("rows",records);
    }

    @ApiOperation(value = "根据ID修改用户信息")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(name = "user", value = "用户对象", required = true)
            @RequestBody User user){
        userService.updateById(user);
        return R.ok()
                .message("修改用户信息成功");
    }


}

